<?php

namespace app\merchant\controller;

use app\common\controller\Tools;
use app\common\model\Enterprise as EnterpriseModel;
use think\Controller;
use app\common\tool\OSSUtils;

class Enterprise extends Controller
{
    public function __construct()
    {
        parent::__construct();
        if (!session('merchant')) {
            $this->redirect('index/login');
        }
    }

    public function upload_enterprise_license_img()
    {
        $whereName = 'enterprise/certificate';
        $column = 'enterprise_license_img_710_400';
        $width = 710;
        $height = 400;
        $data = Tools::upload_img($whereName, $column, $width, $height, true);
        return returnJson(1, '上传成功', $data);
    }

    public function upload_enterprise_idc_up_img()
    {
        $whereName = 'enterprise/idc';
        $column = 'enterprise_idc_up_img_240_150';
        $width = 240;
        $height = 150;
        $data = Tools::upload_img($whereName, $column, $width, $height, true);

        return returnJson(1, '上传成功', $data);;
    }

    public function upload_enterprise_idc_down_img()
    {
        $whereName = 'enterprise/idc';
        $column = 'enterprise_idc_down_img_240_150';
        $width = 240;
        $height = 150;
        $data = Tools::upload_img($whereName, $column, $width, $height, true);
        return returnJson(1, '上传成功', $data);;
    }

    public function upload_enterprise_organization_code_img()
    {

        $whereName = 'enterprise/mulcetificate';
        $column = 'enterprise_organization_code_img_240_150';
        $width = 240;
        $height = 150;
        $data = Tools::upload_img($whereName, $column, $width, $height, true);
        return returnJson(1, '上传成功', $data);;
    }

    public function upload_enterprise_registration_certificate_img()
    {

        $whereName = 'enterprise/mulcetificate';
        $column = 'enterprise_registration_certificate_img_240_150';
        $width = 240;
        $height = 150;
        $data = Tools::upload_img($whereName, $column, $width, $height, true);
        return returnJson(1, '上传成功', $data);;
    }
   //创建方法
    public function create()
    {
        if (!empty(session('merchant'))) {
            $enterprise_id=EnterpriseModel::getEnterpriseId(session('merchant.merchant_id'));
            if (!empty($enterprise_id)) {
                $this->redirect('enterprise/update', ['enterprise_id' => $enterprise_id]);
            }
        }
        return view('create');
    }

    public function insert()
    {
        $tool = new Tools;
        $data = request()->except('province,city,enterprise_license_end_time_1,__token__');
        return $tool->insert('enterprise', $data, 'insert', 'isPost');
    }

    // 修改
    public function update()
    {
        $enterprise_id = input('param.enterprise_id');
        $merchant_id = session('merchant.merchant_id');
        $check_eid = Db("enterprise")->where("enterprise_merchant_id = $merchant_id")->value("enterprise_id");
        if($enterprise_id != $check_eid){
            return returnJson(0, '无权访问');
        }
        $enterprise = EnterpriseModel::find($enterprise_id);
        if (!$enterprise)
            return returnJson(0, '参数错误');
        if (request()->isPost()) {
            $data = request()->except('__token__');

            $data['enterprise_license_location'] = $data['province'] . ',' . $data['city'];
            unset($data['province']);
            unset($data['city']);
            $data['enterprise_license_end_time_1'] = isset($data['enterprise_license_end_time_1']) ? $data['enterprise_license_end_time_1'] : '';
            $data['enterprise_license_reg_time'] = strtotime($data['enterprise_license_reg_time']);
            if ($data['enterprise_license_end_time_1'] == 'on') {
                $data['enterprise_license_end_time'] = 1;
            } else {
                $data['enterprise_license_end_time'] = strtotime($data['enterprise_license_end_time']);
            }
            unset($data['enterprise_license_end_time_1']);
            // 组织机构与代码有效期
            // 1:组织机构与代码有效期不用写入数据库，2：则写入数据库
            if($data['enterprise_is_certificates'] == 1) {
                $data['enterprise_organization_code_reg_time'] = 0;
                $data['enterprise_organization_code_end_time'] = 0;
            }
            if($data['enterprise_is_certificates'] == 2) {
                $data['enterprise_organization_code_reg_time'] = strtotime($data['enterprise_organization_code_reg_time']);
                $data['enterprise_organization_code_end_time'] = strtotime($data['enterprise_organization_code_end_time']);
            }

            $model = new EnterpriseModel;
            $validate = $model->intValidate();
            if (!($validate->scene('update')->check($data)))
                return returnJson(0, $validate->getError());
            if ($enterprise->isUpdate(true)->save($data) !== false) {
                return returnJson(1, '修改成功');
            } else {
                return returnJson(0, '修改失败，请稍后重试！');
            }
        }
        $province = $enterprise['enterprise_license_location'][0];
        $city = $enterprise['enterprise_license_location'][1];
        $enterprise['enterprise_license_reg_time'] = EnterpriseModel::ConvertDate($enterprise['enterprise_license_reg_time']);
        if ($enterprise['enterprise_license_end_time'] > 1) {
            $enterprise['enterprise_license_end_time'] = EnterpriseModel::ConvertDate($enterprise['enterprise_license_end_time']);
        }
        $enterprise['enterprise_organization_code_reg_time'] = EnterpriseModel::ConvertDate($enterprise['enterprise_organization_code_reg_time']);
        $enterprise['enterprise_organization_code_end_time'] = EnterpriseModel::ConvertDate($enterprise['enterprise_organization_code_end_time']);
        return view('update', [
            'province' => $province,
            'city' => $city,
            'enterprise' => $enterprise]);
    }

    public function confirm()
    {
        $data = EnterpriseModel::getEnterpriseAndShop();
        return view('confirm', [
            'enterprise' => $data['enterprise'],
            'storeInfo' => $data['storeInfo'],
        ]);
    }

    /**
     * 图片上传
     */
    public function uploadImg()
    {
        $type = input('type', 0);  //1-营业执照 2-组织机构代码扫描件 3-税务登记证扫描件 4-法人身份证正面 5-法人身份证反面
        if ($type == 1) {
            $imgPath = config('oss_img_path') . 'enterprise/certificate';
            $imgRoot = config('oss_imgRootPath') . 'enterprise/certificate/';
        } elseif (in_array($type, [4, 5])) {
            $imgPath = config('oss_img_path') . 'enterprise/idc';
            $imgRoot = config('oss_imgRootPath') . 'enterprise/idc/';
        } elseif (in_array($type, [2, 3])) {
            $imgPath = config('oss_img_path') . 'enterprise/mulcetificate';
            $imgRoot = config('oss_imgRootPath') . 'enterprise/mulcetificate/';
        } elseif ($type == 6) {
            $imgPath = config('oss_img_path') . 'store/face';
            $imgRoot = config('oss_imgRootPath') . 'store/face/';
        } elseif ($type == 7) {
            $imgPath = config('oss_img_path') . 'store/bg';
            $imgRoot = config('oss_imgRootPath') . 'store/bg/';
        } else {
            return returnJson(0, '图片类型获取失败', '');
        }
        $imgName = input('name');
        if (empty($imgName)) {
            return returnJson(0, '图片名称获取失败', '');
        }
        $oss = new OSSUtils();
        $res = $oss->uploadFile($imgPath."/".$imgName,$_FILES['file']['tmp_name']);
        $res = json_decode($res,true);
        if($res['status'] == 1){
            return returnJson(1, '上传成功', ['imgName' => $imgName, 'imgRoot' => $imgRoot]);
        }else{
            return returnJson(0, $res['msg'], '');
        }
    }
}